package com.NetSunWeiQin.chapter06;

import java.net.*;
import java.io.*;


/**
 * 测试 http连接 属性
 */
public class HttpClient2 {
    public static void main(String args[]) throws IOException {
        URL url = new URL("http://www.javathinker.net/hello.htm");
        URLConnection connection = url.openConnection();

        System.out.println("contentType : " + connection.getContentType());
        System.out.println("getContentLength: " + connection.getContentLength());
        System.out.println("connection.getContentEncoding() = " + connection.getContentEncoding());
        connection.getHeaderFields().forEach(
                (k  , v)-> System.out.println(k +":"+v)
        );
        InputStream in = connection.getInputStream();

        ByteArrayOutputStream buffer = new ByteArrayOutputStream();
        byte[] buff = new byte[1024];
        int len = -1;

        while ((len = in.read(buff)) != -1) {
            buffer.write(buff, 0, len);
        }

        System.out.println(new String(buffer.toByteArray()));

    }
}

